package cn.fws.meap.community.service.impl;

import java.util.*;

import cn.fws.framework.utils.StringUtils;
import cn.fws.meap.community.enums.ResourcesTypeEnum;
import cn.fws.meap.community.service.IEvaluateService;
import cn.fws.meap.community.vo.ReplyListVo;
import cn.fws.meap.system.constants.QiNiuParam;
import cn.fws.meap.system.enums.ReplyTypeEnum;
import cn.fws.meap.utils.QiNiuUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.fws.framework.utils.ConvertUtils;
import cn.fws.framework.utils.UUIDUtils;
import cn.fws.meap.community.mapper.ReplyMapper;
import cn.fws.meap.community.entity.Reply;
import cn.fws.meap.community.entity.Resources;
import cn.fws.meap.community.service.IReplyService;
import cn.fws.meap.community.service.IResourcesService;

import org.springframework.transaction.annotation.Transactional;

/**
 * 回复 服务层实现
 *
 * @author chenkh
 * @date 2019-03-07
 */
@Service
@Transactional
public class ReplyServiceImpl implements IReplyService {

    @Autowired
    private ReplyMapper replyMapper;
    @Autowired
    private IResourcesService resourcesService;
    @Autowired
    private IEvaluateService evaluateService;


    /**
     * 文章详情回复
     *
     * @param articleId
     * @return
     */

    @Override                                                      //flag是来判断是否是二级评论页面
    public List<ReplyListVo> getArticleReply(String articleId, String openId, boolean flag) {
        List<ReplyListVo> twoList = null;
        List<String> list = new ArrayList<>();

        //一级评论
        List<ReplyListVo> replies = replyMapper.selectListReply(articleId);
        for (ReplyListVo reply1 : replies) {
            twoList = new LinkedList<>();

            //一级评论的id存入到list中
            list.add(reply1.getId());
            String fileMsg = QiNiuUtil.getFileMsg(reply1.getReplyMsg());
            reply1.setReplyMsg(fileMsg);

            //查看当前用户对这个评论有没有点过赞
            Boolean aBoolean = evaluateService.selectOpenId(openId, reply1.getId());
            reply1.setEvaluateStatus(aBoolean);
            System.out.println("一级评论id----------" + reply1.getId());

            //判断是否是二级评论的方法
            if (flag||!StringUtils.isNotEmpty(list)) {
                continue;
            }

            //根据一级评论的id 获取到二级评论
            List<ReplyListVo> twoReplies = replyMapper.selectTwoReply(list);
            System.out.println("----------" + twoReplies.toString());

            for (ReplyListVo twoReply : twoReplies) {
                //文章详情页面的二级评论显示三条,定义一个变量count

                System.out.println("二级评论：" + twoReply);
                if (twoReply.getSuperId().equals(reply1.getId())) {

                    String replyMsg2 = twoReply.getReplyMsg();
                    String fileMsg2 = QiNiuUtil.getFileMsg(replyMsg2);
                    twoReply.setReplyMsg(fileMsg2);

                    twoList.add(twoReply);
                }
            }
            reply1.setTwoRepliesList(twoList);
        }


        return replies;
    }

    /**
     * 查询回复信息
     *
     * @param id 回复ID
     * @return 回复信息
     */
    @Override
    public Reply selectById(String id) {
        return replyMapper.selectById(id);
    }

    /**
     * 查询回复列表
     *
     * @param reply 回复信息
     * @return 回复集合
     */
    @Override
    public List<Reply> selectList(Reply reply) {
        return replyMapper.selectList(reply);
    }

    /**
     * 新增回复
     *
     * @param reply 回复信息
     * @return 结果
     */
    @Override
    public int insert(Reply reply) {
        // 设置主键
        reply.setId(UUIDUtils.getUUID());
        // 设置创建时间
        reply.setCreateTime(new Date());
        // 设置是否已经逻辑删除
        reply.setIsDelete(ReplyTypeEnum.IS_DELETE.getCode());
        String replyMsg = reply.getReplyMsg();
        String uploadFile = QiNiuUtil.uploadFile(QiNiuParam.MEAP_DOCUMENT, replyMsg);
        String replyadderssMsg = resourcesService.addResources(uploadFile, ResourcesTypeEnum.TEXT.getCode(),reply.getOpenid());
        reply.setReplyMsg(replyadderssMsg);

        System.out.println("写评论------------------------------------" + reply.toString());
        return replyMapper.insert(reply);
    }

    /**
     * 修改回复
     *
     * @param reply 回复信息
     * @return 结果
     */
    @Override
    public int update(Reply reply) {
        // 设置修改时间
        reply.setUpdateTime(new java.util.Date());
        // 设置是否已经逻辑删除
        reply.setIsDelete(ReplyTypeEnum.NO_DELETE.getCode());
        return replyMapper.update(reply);
    }

    /**
     * 删除回复对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteByIds(String ids) {
        return replyMapper.deleteByIds(ConvertUtils.toStrArray(ids));
    }

}
